/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var detectCycle = function (head) {
  while (head != null) {
    if (!head.tag) {
      head.tag = true;
      head = head.next;
    } else {
      return head;
    }
  }

  return null;
};
